#include "SDL/SDL.h"
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL_image.h>
#include "perso.h"
 int main(int argc, char ** argv)
{
// initialisation des variables
int toucheHaut      = 0;
int toucheGauche  = 0;
int toucheDroite    = 0;
int toucheBas = 0 ;
int jumping=0;
int gravity=10 ;
int i=39,j=0,k=75;
int speed =8;
int persoSurTerre=1;

	int gameloop=1;
	SDL_Surface* ecran=NULL , *image=NULL ;
  SDL_Surface* perso[79];
  SDL_Surface* persoSaut[119];  

personnage1(perso);
per(persoSaut);

image= IMG_Load("STAGE 1.jpg");



for(i=0;i<80;i++)
{
  SDL_SetColorKey(perso[i],SDL_SRCCOLORKEY,SDL_MapRGB(perso[i]->format,255,255,255));
}
for (i = 0; i < 119; i++)
{
  SDL_SetColorKey(persoSaut[i],SDL_SRCCOLORKEY,SDL_MapRGB(persoSaut[i]->format,255,255,255));
}
  
  SDL_Rect posperso	;
  posperso.x=200 ;
  posperso.y=340  ;


SDL_Rect Camera ;
Camera.x=0;
Camera.y=0;
Camera.h=600;
Camera.w=800;


	SDL_Event event ;
    SDL_Init(SDL_INIT_EVERYTHING);
  ecran= SDL_SetVideoMode(800, 600, 32, SDL_DOUBLEBUF | SDL_FULLSCREEN);
SDL_BlitSurface(image,NULL,ecran,NULL);
SDL_BlitSurface(perso[40],NULL,ecran,&posperso);
SDL_Flip(ecran);	
SDL_EnableKeyRepeat(10, 10);
i=40;
while(gameloop)
{
	
 SDL_PollEvent(&event);
	switch(event.type)
	{
		case  SDL_QUIT : 
		gameloop=0;
		break ;
		case SDL_KEYDOWN :
		switch( event.key.keysym.sym)
            {
                  case SDLK_LEFT:
                       toucheGauche = 1;
                       break;
                  case SDLK_RIGHT:
                       toucheDroite  = 1;
                       break;
                  case SDLK_UP:
                       toucheHaut = 1;
                       break;
                  case SDLK_DOWN:
                        toucheBas = 1;
                        break;
             }
             break;
		case SDL_KEYUP:
            switch( event.key.keysym.sym)
            {
                  case SDLK_LEFT:
                       toucheGauche = 0;
                       i=39;                      
                       break;
                  case SDLK_RIGHT:
                       toucheDroite = 0;
                      i=40;
                       break;
                   case SDLK_UP:
                       toucheHaut = 0;
                       break;
                  case SDLK_DOWN:
                        toucheBas = 0;
                        break;
             }
             break;
	

  }
if ( toucheHaut ) 
{
      if( persoSurTerre )
      { 
      j=10;
      }
 
} 
else if ( toucheBas )
 {                   
 }
  if( toucheGauche ) {
            
          //  posperso.x --; // Vitesse déplacement gauche   
            Camera.x -= speed;
                  if(Camera.x <=0)
              Camera.x=11340-800;
            
                 i--;
             if(i<=0)
              {
                i=39;
              }
             
     }
      else if( toucheDroite ) 
          {
            
            // posperso.x++ ; // Vitesse déplacement droite
            Camera.x += speed;
            if(Camera.x >=11340-800)
              Camera.x=0;
             i++;
             if(i>=79)
              {
                i=40;
              }
          }

if(j!=0)
{posperso.y-=40; 
persoSurTerre=0;
j--;
}

if(persoSurTerre==0)
{
  posperso.y +=gravity;
}

if(posperso.y>=340)
{
  persoSurTerre=1;
}


if(posperso.x>720)
 { posperso.x=720 ;}
if(posperso.y>400)
 { posperso.y=400 ;}
             //  SDL_FreeSurface(perso[i]);
              SDL_BlitSurface(image,&Camera,ecran,NULL);
              
              if(persoSurTerre==0)
              {
                k++;
              if(k>=119)
                {
                  k=80;
                }
              SDL_BlitSurface(persoSaut[k],NULL,ecran,&posperso);
              }else
              {
              SDL_BlitSurface(perso[i],NULL,ecran,&posperso);                
              }
              SDL_Delay(10);
              SDL_Flip(ecran);
}
  SDL_Quit();
	return EXIT_SUCCESS;
}